package cn.javaxc.mvcwork.controller;
import cn.javaxc.mvcwork.controller.MVCConstant;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import cn.javaxc.mvcwork.framework.database.ConnectionElement;
import cn.javaxc.mvcwork.modules.admin.User;
import cn.javaxc.model.base.*;
import java.io.File;
import java.sql.*;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.w3c.dom.*;
import java.util.Date;
import org.apache.ibatis.session.SqlSessionFactory;

//import net.sf.asterisk.manager.ManagerConnection;
import org.apache.log4j.Logger;
//import org.jivesoftware.smack.XMPPConnection;
//import cn.javaxc.mvcwork.util.contact.XMPPManager;
import cn.javaxc.mvcwork.util.helper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


public class SystemStatus {
  //Unique to this system
  private ConnectionElement connectionElement = null;
  private String fileLibraryPath = null;
  private String url = null;


  //Site Preferences
  private int sessionTimeout = 60*60*10;//秒为单位

  //Access to applicationPrefs (readOnly)
  protected MVCConfig applicationPrefs = null;


  //System Language
  private String language = null;
  private SQLList sysList=new SQLList();

  // Logger.
  private static final Logger LOGGER = Logger.getLogger(SystemStatus.class);
  //XML Object Map (readOnly)
//  SyncTableList systemObjectMap = null;


    //Session Manager
  private SessionManager sessionManager = new SessionManager();





  //Portal Tracker
  private Tracker tracker = new Tracker();


//  private ManagerConnection asteriskConnection = null;
//  private XMPPConnection xmppConnection = null;



  //XML Object Map (readOnly)
//  SyncTableList systemObjectMap = null;

  public SystemStatus() {
  }


  
  public SystemStatus(ServletContext context){
    queryStatus(context);
  }


  
  public void queryStatus(ServletContext context){

//	loadContainerMenu(context);

	buildGlobalParams(context);
  }



  
  public void setConnectionElement(ConnectionElement tmp) {
    this.connectionElement = tmp;
  }


  
  public void setFileLibraryPath(String tmp) {
    this.fileLibraryPath = tmp;
  }


  
  public String getUrl() {
    return url;
  }


  
  public void setUrl(String url) {
    this.url = url;
  }




  
  public void setSessionTimeout(int sessionTimeout) {
    this.sessionTimeout = sessionTimeout;
  }





  
  public int getSessionTimeout() {
    return sessionTimeout;
  }

  public Hashtable getUserList() {
    return null;
  }


  
  public String getLabel(String thisLabel) {
    String text = this.getValue("system.fields.label", thisLabel);
    if (text == null&&applicationPrefs!=null) {
      text = applicationPrefs.getLabel("system.fields.label", thisLabel, language);
    }
    return text;
  }


  
  public String getLabel(String thisLabel, String defaultText) {
    String result = getLabel(thisLabel);
    if (result == null) {
      return defaultText;
    } else {
      return result;
    }
  }


  
  public String[] getLettersArray(String thisLabel) {
    String letters = this.getLabel(thisLabel);
    return letters.split(",");
  }


  
  public String getMenuProperty(String item, String thisProperty) {
    String text = this.getValue("system.modules.label", item, thisProperty);
    if (text == null) {
      text = applicationPrefs.getValue(
          "system.modules.label", item, thisProperty, language);
    }
    return text;
  }


  
  public String getSubMenuProperty(String thisLabel) {
    String text = this.getValue("system.submenu.label", thisLabel);
    if (text == null) {
      text = applicationPrefs.getLabel("system.submenu.label", thisLabel, language);
    }
    return text;
  }


  
  public String getContainerMenuProperty(String collection, String thisProperty) {
    String text = this.getValue(collection, thisProperty, "value");
    if (text == null) {
      text = applicationPrefs.getValue(collection, thisProperty, "value", language);
    }
    return text;
  }


  
  public ConnectionElement getConnectionElement() {
    return connectionElement;
  }


  
  public String getFileLibraryPath() {
    return fileLibraryPath;
  }




  
  public MVCConfig getFumineConfig() {
    return applicationPrefs;
  }




  
  public void setFumineConfig(MVCConfig tmp) {
    this.applicationPrefs = tmp;
  }


  
  public String getLanguage() {
    return language;
  }


  
  public void setLanguage(String language) {
    this.language = language;
  }
  
  public String getValue(String section, String parameter) {
    return getValue(section, parameter, "value");
  }


  
  public String getValue(String section, String parameter, String tagName) {
    return parameter;
  }


  
  public int getValueAsInt(String section, String parameter) {
    String intValue = this.getValue(section, parameter);
    if (intValue == null) {
      return -1;
    } else {
      return Integer.parseInt(intValue);
    }
  }

 


	@Autowired
	private SqlSessionFactory sqlSessionFactory;
	public SqlSessionFactory getSqlSessionFactory(){
		return this.sqlSessionFactory;
	}
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory=sqlSessionFactory;
	}
	
	@Autowired
	@Qualifier("baseDAO")
	IBaseDAO dao;
	public IBaseDAO getBaseDAO(){
		return this.dao;
	}
	public void setBaseDAO(IBaseDAO dao){
		this.dao=dao;
	}

  /**
   * 生成网站底部的公司名
   */  
  public void buildGlobalParams(ServletContext context)
  {

	 SQLList<SQLMap> s_sys_info = new SQLList();
	try{


		s_sys_info=dao.buildGlobalParams(this);
		sysList=dao.buildSysList(this);
	}catch(Exception e){
		e.printStackTrace();
	}
	
	buildGlobalParams(s_sys_info,context);

  }
  public void setSysList(SQLList<SQLMap> sysList)
  {
	 this.sysList=sysList;
  }
  public String getSysList(String table){
	 if (sysList.fromCode(table)!=null){
		return table;
	 }
	 throw new IllegalArgumentException(table);
  }
  public void buildGlobalParams(SQLList<SQLMap> s_sys_info,ServletContext context)
  {


	for (SQLMap emt:s_sys_info ){
		if (!emt.getCode().toString().contains(":")){
			applicationPrefs.add((String)emt.getCode(),emt.getDescription());
		}else{
			applicationPrefs.addClassProperty((String)emt.getCode(),emt.getDescription());
		}
	}
     context.setAttribute("APP_ORGANIZATION",applicationPrefs.get("app_text"));

  }



  
  public Tracker getTracker() {
    return tracker;
  }


//
//  
//  public ManagerConnection getAsteriskConnection() {
//    return asteriskConnection;
//  }
//
//
// 
//  
//  public XMPPConnection getXmppConnection() {
//    return xmppConnection;
//  }
//
//
//
//  
//  public void startServers(ServletContext context) {
//    // Monitor Jabber
//    if ("true".equals(applicationPrefs.get("XMPP.ENABLED"))) {
//      xmppConnection = XMPPManager.verifyConnection(this, applicationPrefs);
//    } else {
//      XMPPManager.removeConnection(this);
//    }
//  }
//
//
//  
//  public void stopServers() {
//    //Unload XMPPConnection
//    XMPPManager.removeConnection(this);
//  }

 
  
  public SessionManager getSessionManager() {
    return sessionManager;
  }


  public String toString(){
	return connectionElement!=null?connectionElement.getUrl():super.toString();
  }
}
